package com.streambolics.worseminton.data;

import java.util.Calendar;

import javax.jdo.PersistenceManager;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.Inheritance;
import javax.jdo.annotations.InheritanceStrategy;
import javax.jdo.annotations.NotPersistent;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;

import com.streambolics.data.Agent;
import com.streambolics.std.HtmlBuilder;

@PersistenceCapable(identityType = IdentityType.APPLICATION)
@Inheritance(strategy = InheritanceStrategy.NEW_TABLE)
public class Location extends Agent
{
    @Persistent
    private Long                 ownerId;

    @Persistent
    private Double               courtPrice;

    @NotPersistent
    private LocationInterestList _Interests;

    /**
     * Loads a location given its unique id
     * 
     * @param aDatabase
     * @param aId
     * @return
     */

    public static Location load (PersistenceManager aDatabase, long aId)
    {
        Location l = aDatabase.getObjectById (Location.class, aId);
        l.setDatabase (aDatabase);
        return l;
    }

    public void generateGoogleMap (HtmlBuilder aBuilder)
    {
        double lat = getLatitude ();
        double lng = getLongitude ();
        if (lat != 0 && lng != 0)
        {
            aBuilder.addGoogleMap (lat, lng, getName ());
        }

    }

    public void generateNewEventAnchor (HtmlBuilder aBuilder)
    {
        aBuilder.addAnchor (getNewEventURL (), getNewEventCaption ());
    }

    public double getCourtPrice ()
    {
        if (courtPrice == null)
        {
            return 0.0;
        }
        else
        {
            return courtPrice;
        }
    }

    public String getCourtPriceAsText ()
    {
        if (courtPrice == null)
        {
            return "";
        }
        else
        {
            return Double.toString (courtPrice);
        }
    }

    /**
     * Creates a new template for an event. The template is not made persistent.
     * 
     * @return
     */

    public Event getEventTemplate ()
    {
        Event e = new Event ();
        e.setCalendar (getNextDate ());
        e.setLocationId (getId ());
        e.setDatabase (getDatabase ());
        return e;
    }

    public LocationInterestList getInterests ()
    {
        if (_Interests == null)
        {
            _Interests = LocationInterestList.load (getDatabase (), this);
        }
        return _Interests;
    }

    public String getNewEventCaption ()
    {
        return "Create new event at " + getName ();
    }

    public String getNewEventURL ()
    {
        return "/go/EventEnter?ID=" + getIdAsString ();
    }

    public Calendar getNextDate ()
    {
        return createCalendar ();
    }

    public Long getOwnerId ()
    {
        return ownerId;
    }

    public void setCourtPrice (double aCourtPrice)
    {
        courtPrice = aCourtPrice;
    }

    public void setOwnerId (Long ownerId)
    {
        this.ownerId = ownerId;
    }
}
